package day18;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Demo5 {
    ReentrantLock lock = new ReentrantLock(true);
    ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    public void test(){
        String name = Thread.currentThread().getName();
        try {
            lock.lock();//添加lock互斥锁
            System.out.println(name + "获取了锁，开始执行...");
            Thread.sleep(1000);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            lock.unlock();
            System.out.println(name);
        }
    }
    public static void main(String[] args) {
        Demo5 demo5 = new Demo5();
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                while (true){
                    demo5.test();
                }
            },"线程:"+i).start();
        }
    }
}
